/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.base.stack
 * author:David
 * Date:2015年6月13日下午8:53:20
 * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.base.stack;

/**
 * 栈的链式存储实现
 * @author David
 * @since 2015年6月13日 下午8:53:20
 * @version 
 * @since JDK 1.6
 */
public class StackLink implements Stack{

	private SLNode top;//链表头结点引用
	private SLNode tair=new SLNode();
	private int size;  //栈的大小
	
	public StackLink() {
		top=null;
		tair=top;
		size=0;
	}


	//返回栈大小
	@Override
	public int getSize() {
		// TODO Auto-generated method stub
		return size;
	}


	//判断栈是否为空
	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return size==0;
	}

	//数据元素e入栈
	@Override
	public void push(Object e) {
		SLNode node=new SLNode();
		node.setData((Integer) e);
		if(top==null){
			node.setNext(null);
		}
		node.setNext(top);
		top=node;
		size++;
	}

	//栈顶元素出栈
	@Override
	public Integer pop() throws StackEmptyException {
		if(size<1){
			throw new StackEmptyException("错误，栈为空！");
		}
		Integer obj=top.getData();
		top=top.getNext();
		size--;
		return obj;
	}

	@Override
	public Integer peek() throws StackEmptyException {
		if(size<1){
			throw new StackEmptyException("错误，栈为空！");
		}
		return top.getData();
	}
	
}


class SLNode{
	private Integer data;
	private SLNode next;

	public Integer getData() {
		return data;
	}
	public void setData(Integer data) {
		this.data = data;
	}
	public SLNode getNext() {
		return next;
	}
	public void setNext(SLNode next) {
		this.next = next;
	}
	
}
